package com.mtt.erp.goods.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.goods.dto.MaaCreateDto;
import com.mtt.erp.goods.dto.MaaDto;
import com.mtt.erp.goods.dto.MaaPageDto;
import com.mtt.erp.goods.dto.MaaUpdateDto;
import com.mtt.erp.goods.service.IMaaFileService;
import com.mtt.erp.goods.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/maa")
@Api(value = "商品接口",tags = "M001-商品库-主商品信息相关接口")
public class MaaFileController {
    @Autowired
    private IMaaFileService iMaaFileService;

    @Log("查询商品")
    @GetMapping
    // @RequiresPermissions("maa:view")
    @ApiOperation(value = "查询商品",notes = "分页查询")
    public R<IPage<MaaPageDto>> maaList(QueryRequest request, MaaDto maaDto) {
        IPage<MaaPageDto> maasIpage = this.iMaaFileService.findMaa(request, maaDto);
        // 判断结果数
        if (maasIpage.getTotal()>0){
            return R.data(maasIpage);
        } else {
            return R.fail(ResultCode.NO_CONTENT);
        }
    }


    //@Log("添加商品")
    @PostMapping
    //@RequiresPermissions("maa:add")
    @ApiOperation(value = "添加商品",notes = "添加商品,传入商品属性值")
    public R createMaa(@Valid MaaCreateDto dto){
        try {
            if (this.iMaaFileService.createMaa(dto)){
                return R.success(ResultCode.SUCCESS);
            } else {
               return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }

    }

    // @Log("修改商品")
    @PutMapping
    @RequiresPermissions("maa:update")
    @ApiOperation(value = "修改商品",notes = "修改商品,传入商品属性值")
    public R updateMaa(@Valid MaaUpdateDto maaFile){
        try {
            if (this.iMaaFileService.updateMaa(maaFile)){
               return R.success(ResultCode.SUCCESS);
            } else{
              return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }


    @GetMapping("beforeUpdateInit")
    @ApiOperation(value = "初始化商品信息",notes = "修改时初始化商品信息,传入修改商品的id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id(maa00)",required = true,paramType = "query")
    })
    public R<GoodsVo> beforeUpdateInit(@NotBlank(message = "{required}") String id) {
        GoodsVo goodsVo = this.iMaaFileService.beforeEditInit(id);
        if (null != goodsVo) {
            return R.data(goodsVo);
        } else {
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    @PutMapping("putAway")
    @ApiOperation(value = "上架商品",notes = "批量上架商品,传入商品的id串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "商品id串,逗号分隔(maa00)",required = true,paramType = "query")
    })
    public R putAway(@NotBlank(message = "{required}") String ids){
        String[] maaIds = ids.split(StringPool.COMMA);
        try {
            Boolean putAway = this.iMaaFileService.putAway(maaIds);
            if(putAway){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    @PutMapping("soldOut")
    @ApiOperation(value = "下架商品",notes = "批量下架商品,传入商品的id串")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "商品id串,逗号分隔(maa00)",required = true,paramType = "query")
    })
    public R soldOut(@NotBlank(message = "{required}") String ids){
        String[] maaIds = ids.split(StringPool.COMMA);
        try {
            Boolean soldOut = this.iMaaFileService.soldOut(maaIds);
            if(soldOut){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }

    }

    @GetMapping("/check/{maa01}")
    @ApiOperation(value = "SKU唯一校验", notes = "校验SKU不重复,传入maa01")
    @ApiImplicitParams({
            @ApiImplicitParam(name="maa01",value="商品SKU",required=true,paramType = "path")
    })
    public R checkSKU(@NotBlank(message = "{required}") @PathVariable String maa01) {
        if(null == this.iMaaFileService.getBymaa01(maa01)){
            return R.success("SKU未被使用");
        }else{
            return R.fail("SKU已存在");
        }
    }

    @GetMapping("/getSKU")
    @ApiOperation(value = "获取商品SKU", notes = "自动生成商品SKU")
    public R getSKU() {
        if(null != this.iMaaFileService.getSKU()){
           return R.data(this.iMaaFileService.getSKU());
        }else{
            return R.fail(ResultCode.FAILURE);
        }
    }
}
